package com.ruoyi.report_lq.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_lq.domain.LqDeviceInfo;
import com.ruoyi.report_lq.domain.LqsdhrdjlbBsInfo;
import com.ruoyi.report_lq.domain.bo.LqDeviceBo;
import com.ruoyi.report_lq.domain.bo.LqsdhrdjlbBsInfoBo;
import com.ruoyi.report_lq.domain.vo.LqDeviceVo;
import com.ruoyi.report_lq.domain.vo.LqsdhrdjlbBsInfoVo;
import com.ruoyi.report_lq.mapper.LqdlsybgDeviceMapper;
import com.ruoyi.report_lq.mapper.LqsdhrdjlbBsInfoMapper;
import com.ruoyi.report_lq.service.ILqsdhrdjlbBsInfoService;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import com.ruoyi.syrw.domain.vo.BsDataReportVo;
import com.ruoyi.syrw.service.IBsDataReportService;
import com.ruoyi.system.service.ISysDeptService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_lq.domain.bo.LqdlsybgBsInfoBo;
import com.ruoyi.report_lq.domain.vo.LqdlsybgBsInfoVo;
import com.ruoyi.report_lq.domain.LqdlsybgBsInfo;
import com.ruoyi.report_lq.mapper.LqdlsybgBsInfoMapper;
import com.ruoyi.report_lq.service.ILqdlsybgBsInfoService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 沥青道路石油报告Service业务层处理
 *
 * @author zyb
 * @date 2024-05-14
 */
@RequiredArgsConstructor
@Service
public class LqdlsybgBsInfoServiceImpl implements ILqdlsybgBsInfoService {

    private final LqdlsybgBsInfoMapper baseMapper;

    private final ISysDeptService deptService;

    private final IBsDataReportService iBsDataReportService;

    private final ILqsdhrdjlbBsInfoService iLqsdhrdjlbBsInfoService;

    private final LqsdhrdjlbBsInfoMapper lqsdhrdjlbBsInfoMapper;

    private final LqdlsybgDeviceMapper deviceMapper;

    /**
     * 查询沥青道路石油报告
     */
    @Override
    @FIllBasicInfo
    public LqdlsybgBsInfoVo queryById(Long excelId){
        return baseMapper.selectVoById(excelId);
    }



    /**
     * 查询沥青道路石油报告列表
     */
    @Override
    public TableDataInfo<LqdlsybgBsInfoVo> queryPageList(LqdlsybgBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<LqdlsybgBsInfo> lqw = buildQueryWrapper(bo);
        Page<LqdlsybgBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 根据reportId查询 试验记录表信息
     */
    @Override
    public List<LqdlsybgBsInfoVo> queryList(LqdlsybgBsInfoBo bo) {
        LambdaQueryWrapper<LqdlsybgBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<LqdlsybgBsInfo> buildQueryWrapper(LqdlsybgBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<LqdlsybgBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), LqdlsybgBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), LqdlsybgBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), LqdlsybgBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpxx()), LqdlsybgBsInfo::getYangpxx, bo.getYangpxx());
        lqw.eq(bo.getYangpxxid() != null, LqdlsybgBsInfo::getYangpxxid, bo.getYangpxxid());
        lqw.eq(bo.getShiyjcrq() != null, LqdlsybgBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytj()), LqdlsybgBsInfo::getShiytj, bo.getShiytj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), LqdlsybgBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), LqdlsybgBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), LqdlsybgBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        return lqw;
    }

    /**
     * 新增沥青道路石油报告
     */
    @Override
    @Transactional
    public Boolean insertByBo(LqdlsybgBsInfoBo bo) {
        LqdlsybgBsInfo add = BeanUtil.toBean(bo, LqdlsybgBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0; //记录主表
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        //TODO 设备数据保存
        List<LqDeviceBo> deviceArr = bo.getDeviceArr();
        if(!deviceArr.isEmpty()) {
            deviceArr.forEach(device-> {
                saveDevice(device,bo.getExcelId());
            });
        }
        //TODO 闪点数据保存
        LqsdhrdjlbBsInfoBo sdInfoBo = BeanUtil.toBean(bo.getSdBsInfoVo(),LqsdhrdjlbBsInfoBo.class);
//        sdInfoBo.setLqsdhrdjlbid(bo.getExcelId());
        flag = iLqsdhrdjlbBsInfoService.insertByBo(sdInfoBo);
        return flag;
    }

    /**
     * 修改沥青道路石油报告
     */
    @Override
    public Boolean updateByBo(LqdlsybgBsInfoBo bo) {
        boolean flag;
        LqdlsybgBsInfo update = BeanUtil.toBean(bo, LqdlsybgBsInfo.class);
        validEntityBeforeSave(update);
        flag = baseMapper.updateById(update) > 0;

        //TODO 删除原有设备数据
        List<LqDeviceBo> oldDeviceArr = queryByJlId(bo.getExcelId());
        oldDeviceArr.forEach(oldArr -> {
            QueryWrapper<LqDeviceInfo> query = new QueryWrapper<>();
            query.eq("device_id", oldArr.getDeviceId());
            deviceMapper.delete(query);
        });
        //TODO 设备数据更新
        List<LqDeviceBo> deviceArr = bo.getDeviceArr();
        deviceArr.forEach(device-> {
            saveDevice(device,bo.getExcelId());
        });
        //TODO 闪点数据更新
        LqsdhrdjlbBsInfoBo sdInfoBo = BeanUtil.toBean(bo.getSdBsInfoVo(),LqsdhrdjlbBsInfoBo.class);
        flag = iLqsdhrdjlbBsInfoService.updateByBo(sdInfoBo);
        return flag;
    }

    /**
     * @method 共用设备存储方法
     * @param dBo
     * @param pId
     */
    public int saveDevice(LqDeviceBo dBo, Long pId) {
        LqDeviceInfo deviceInfo = new LqDeviceInfo();
        deviceInfo.setDeviceLabel(dBo.getLabel());
        deviceInfo.setDeviceKey(dBo.getKey());
        deviceInfo.setLqjlId(pId);
        //TODO 新增数据
        long deviceId = IdUtil.getSnowflakeNextId();
        deviceInfo.setDeviceId(deviceId);
        return deviceMapper.insert(deviceInfo);
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(LqdlsybgBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
        //TODO 根据reportId 查询记录主表中是否已经包含有当前记录的数据
    }

    /**
     * 批量删除沥青道路石油报告
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }


    /**
     * 沥青闪点与燃点试验记录表----> 表单数据获取
     */

    public LqdlsybgBsInfoVo getJlInfo(Long deptId, Long reportId) {
        // 获取工程名称、检测单位名称
        SysDept sysDept = deptService.selectDeptById(deptId);
        // 获取记录编号、样品信息等
        BsDataReportVo bsReportVo = iBsDataReportService.queryById(reportId);

        // 闪点、燃点 信息

        LqsdhrdjlbBsInfoVo sdBsInfoVo = new LqsdhrdjlbBsInfoVo();


        LqdlsybgBsInfoVo lqbsInfoVO = new LqdlsybgBsInfoVo();
        lqbsInfoVO.setGongcmc(sysDept.getConstructionProject());
        lqbsInfoVO.setJiancdwmc(sysDept.getTestingCompany());
        lqbsInfoVO.setJilbh(bsReportVo.getJlCode());
        lqbsInfoVO.setGongcbwyt(bsReportVo.getYpVo().getGcbw());
        // 组装样品信息
        StringBuilder builder = new StringBuilder();
        builder.append("样品名称： ");
        builder.append(bsReportVo.getYpVo().getYpClass());
        builder.append(" 样品编号： ");
        builder.append(bsReportVo.getYpVo().getYpNum());
        builder.append(" 样品状态： ");
        builder.append(bsReportVo.getYpVo().getYpDescribe());

        lqbsInfoVO.setYangpxxid(bsReportVo.getYpVo().getYpId());//样品信息ID
        lqbsInfoVO.setYangpxx(builder.toString());
        lqbsInfoVO.setYpCode(bsReportVo.getYpVo().getYpNum());
        lqbsInfoVO.setYpClass(bsReportVo.getYpVo().getYpClass());
        lqbsInfoVO.setYpDescribe(bsReportVo.getYpVo().getYpDescribe());

        lqbsInfoVO.setBsYpVo(bsReportVo.getYpVo());
        lqbsInfoVO.setSdBsInfoVo(sdBsInfoVo);

        return lqbsInfoVO;
    }


    /**
     * 根据reportId 查询记录表数据
     */
    @Override
    public LqdlsybgBsInfoVo queryByReportId(Long reportId){
        QueryWrapper<LqdlsybgBsInfo> query = new QueryWrapper<>();
        query.eq("report_id", reportId);
        LqdlsybgBsInfoVo infoVo = baseMapper.selectVoOne(query);
        if(!BeanUtil.isEmpty(infoVo)) {  // 判断是否为新增记录表
            // 查询设备信息
            infoVo.setDeviceArr(queryByJlId(infoVo.getExcelId()));
            // 查询闪点信息
            infoVo.setSdBsInfoVo(quereyRdByJlid(infoVo.getExcelId()));
        }
        return infoVo;
    }

    /**
     * 获取设备信息
     */
    public List<LqDeviceBo> queryByJlId(Long lqjlId) {
        QueryWrapper<LqDeviceInfo> query = new QueryWrapper<>();
        query.eq("lqjl_id", lqjlId);
        List<LqDeviceVo> voList = deviceMapper.selectVoList(query);
        List<LqDeviceBo> boList = new ArrayList<>();
        voList.forEach(vo -> {
            LqDeviceBo bo = new LqDeviceBo();
            bo.setKey(vo.getDeviceKey());
            bo.setLabel(vo.getDeviceLabel());
            bo.setLqjlId(vo.getLqjlId());
            bo.setDeviceId(vo.getDeviceId());
            boList.add(bo);
        });
        return boList;
    }

    /**
     * 获取当前记录表中的闪点、燃点信息
     */
    public LqsdhrdjlbBsInfoVo quereyRdByJlid(Long lqjlId) {
        QueryWrapper<LqsdhrdjlbBsInfo> query = new QueryWrapper<>();
        query.eq("lqsdhrdjlbid", lqjlId);
        return lqsdhrdjlbBsInfoMapper.selectVoOne(query);
    };
}















